<?php 
namespace Album\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Album\Model\Album;
use Album\Form\AlbumForm;
use Zend\Db\Adapter\Adapter;
use Zend\ServiceManager;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use Album\Model\DbConnect;
class AlbumController extends AbstractActionController 
{ 
    protected $albumTable;


    public function indexAction() 
    { 
          
        // grab the paginator from the AlbumTable
     $paginator = $this->getAlbumTable()->fetchAll(true);
     // set the current page to what has been passed in query string, or to 1 if none set
     $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page',1));
     
     // set the number of items per page to 10
     $paginator->setItemCountPerPage(10)->setPageRange(7);
     

     return new ViewModel(array(
         'paginator' => $paginator
     ));
        
       
    } 
    public function editAction(){
        $vm = new ViewModel();
         $vm->setTemplate('Album/album/edit.phtml');
        $id = (int) $this->params()->fromRoute('id',0);
        
        
        if(!$id){
            return $this->redirect()->toRoute('album',array(
                'action' => 'add',
            ));
        }       
            $album = $this->getAlbumTable()->getAlbum($id);
       $dbAdapter = $this->getServiceLocator()->get('db_adapter');
        $form      = new AlbumForm($dbAdapter);
        //$form = new AlbumForm();
        $form->bind($album);
        $form->get('submit')->setAttribute('value', 'Edit');
        $request = $this->getRequest();
        
        if($request->isPost()){
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());
            if($form->isValid()){
                $this->getAlbumTable()->saveAlbum($form->getData());
                return $this->redirect()->toRoute('album');
                
            }
        }
  
        return new ViewModel(array(
            'id'    => $id,
            'form'  => $form
        ));
    }
    public  function addAction(){
        
        //$db = $this->getAdapter();// use this method required declare function getAdapter();//line bottom
     /*   $db = $this->getServiceLocator()->get('db_adapter');//get connect info ussing serviceLocator
        echo "<pre>";
        print_r($db);
        echo "</pre>";
        $sql ="select * from album";
        
        $list = $db->getDriver()->getConnection()->execute($sql);        //excute query
        $a =array();
        //foreach to list array
        if($list->count() > 0){
            $results = new ResultSet();
            $a= $results->initialize($list)->toArray();
        }
        echo "<pre>";
        print_r($a[2]['id']);
        echo "</pre>";
      * 
      */
        $dbAdapter = $this->getServiceLocator()->get('db_adapter');
        $form      = new AlbumForm($dbAdapter);
    
        //$form = new AlbumForm();
        $form->get('submit')->setValue('Add');
        $request = $this->getRequest();        
        
        if ($request->isPost()) {
            $album = new Album();
            
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());
            if ($form->isValid()) {
                $album->exchangeArray($form->getData());
                $this->getAlbumTable()->saveAlbum($album);
        // Redirect to list of albums
                return $this->redirect()->toRoute('album');
            }
        }
        return array('form' => $form);
    }
    public function deleteAction() {
        $id = (int)$this->params()->fromRoute('id',0);
        if(!$id){
            return  $this->redirect()->toRoute('album');
        }
        $request = $this->getRequest();
        if($request->isPost()){
            $del = $request->getPost('del','No');
            if($del == 'Yes'){
                $id = (int) $request->getPost('id');
                $this->getAlbumTable()->deleteAlbum($id);
            }
            return $this->redirect()->toRoute('album');
        }
        return array(
            'id' => $id,
            'album' => $this->getAlbumTable()->getAlbum($id)
        );
    }
    public function getAlbumTable(){
        if(!$this->albumTable){
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }
    public function getAdapter()
    {
       if (!$this->adapter) {
          $sm = $this->getServiceLocator();
          $this->adapter = $sm->get('Zend\Db\Adapter\Adapter');
       }
       return $this->adapter;
    }
    public function testAction(){
        $vm = new ViewModel();
        $vm->setTemplate('album/album/test.phtml');

        $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $form      = new AlbumForm($dbAdapter);

        return $vm->setVariables(array(
            'form' => $form
        ));
    }
}